package com.baitengsoft.patentapimonitor.api.controller;

/**
 * @author Chen 2018/1/31
 */

import com.baitengsoft.patentapimonitor.api.entity.WebUserinfoEntity;
import com.baitengsoft.patentapimonitor.api.model.*;
import com.baitengsoft.patentapimonitor.api.model.dto.*;
import com.baitengsoft.patentapimonitor.api.service.RptApplicantService;
import com.baitengsoft.patentapimonitor.api.service.RptPatentDataAreaService;
import com.baitengsoft.patentapimonitor.api.service.WebExcelExportService;
import com.baitengsoft.patentapimonitor.common.utils.DateUtils;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import com.baitengsoft.patentapimonitor.common.utils.WebExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Chen 2018/1/30
 */
@RestController
@RequestMapping("api/v1/web/analysisexcel")
@Api(value = "前台-统计分析-导出Excel",description = "统计分析")
public class WebAnalysisExcelExportController extends AbstractController {
    @Autowired
    private RptPatentDataAreaService rptPatentDataAreaService;
    @Autowired
    private RptApplicantService rptApplicantService;
    @Autowired
    private WebExcelExportService webExcelExportService;
    /**
     * 历年申请趋势
     */
    @ApiOperation(value ="历年申请趋势汇总/type 0.申请趋势,1 授权趋势")
    @GetMapping("/trendanalysis/{type}")
    @ResponseBody
    public Res geTrendAnalysisList(@PathVariable(value = "type") Integer type, HttpServletResponse response) throws Exception{
        if(type == null){
            return Res.error("参数有误");
        }
        if(type !=0 && type !=1){
            return Res.error("趋势参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("未查询到平台数据");
        }
        List<WebYearStatisticInfo> infos = rptPatentDataAreaService.getTrendAnalysisList(userInfo.getPlatformid(), type);
        List<WebYearStatisticInfoDto> webDtos = webExcelExportService.getWebYearStatisticInfoDto(infos);
        String title;
        if (type.equals(0)){
            title = "历年申请趋势汇总";
        }else {
            title = "历年授权趋势汇总";
        }
        return WebExcelUtils.exportGeneralExcelFile(
                webDtos,
                response,
                DateUtils.getCurrentYear()+"年"+title+".xls",

                DateUtils.getCurrentYear()+"年"+title);
    }

    /**
     * 授权专利月度统计
     */
    @ApiOperation(value ="授权专利月度统计/type 0.申请趋势,1 授权趋势")
    @GetMapping("/trenddetail/{type}")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "year", value = "年份", required = true),
    })
    public Res getTrendDetailList(@PathVariable(value = "type") Integer type,Integer year,HttpServletResponse response) throws Exception {
        if (type == null || year == null) {
            return Res.error("参数有误");
        }
        if (type != 0 && type != 1) {
            return Res.error("趋势参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo == null || userInfo.getPlatformid() == null) {
            return Res.error("未查询到平台数据");
        }
        List<WebYearStatisticInfo> infos = rptPatentDataAreaService.getTrendDetailList(userInfo.getPlatformid(), type, year);
        List<WebYearStatisticInfoDto> webDtos = webExcelExportService.getWebYearStatisticInfoDto(infos);
        String title = year + "年申请专利月度统计";
        if (type == 1)
            title = year + "年授权专利月度统计";
        return WebExcelUtils.exportGeneralExcelFile(
                webDtos,
                response,
                title + ".xls",
                title);
    }

    /**
     * 有效专利区域统计
     */
    @ApiOperation(value ="有效专利区域统计")
    @GetMapping("/areaAnalysis")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sort", value = "排序列", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "order", value = "排序方式[desc asc]", required = false)
    })
    public Res getAreaAnalysislList(String order,String sort,HttpServletResponse response) throws Exception{
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("未查询到平台数据");
        }
        List<WebAreaStatisticInfo> infos = rptPatentDataAreaService.getAreaAnalysislList(userInfo.getPlatformid(), order, sort);
        List<WebAreaStatisticInfoDto> webDtos = webExcelExportService.getWebAreaStatisticInfoDto(infos);
        return WebExcelUtils.exportGeneralExcelFile(
                webDtos,
                response,
                DateUtils.getCurrentYear()+"年有效专利区域统计.xls",
                DateUtils.getCurrentYear()+"年有效专利区域统计");
    }

    /**
     * IPC分类统计
     */
    @ApiOperation(value ="IPC分类统计")
    @GetMapping("/ipcAnalysis")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sort", value = "排序列 yxfmcount(有效发明)/yxsycount(有效实用)/yxwgcount(有效外观)/yxhzcount（合计）", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "order", value = "排序方式[desc asc]", required = false)
    })
    public Res getIPCAnalysislList(String order,String sort,HttpServletResponse response) throws Exception{
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("未查询到平台数据");
        }
        List<WebIPCAnalysisInfo> infos = rptPatentDataAreaService.getIPCAnalysisList(userInfo.getPlatformid(), order, sort);
        List<WebIPCAnalysisInfoDto> webDtos = webExcelExportService.getWebIPCAnalysisInfoDto(infos);
        return WebExcelUtils.exportGeneralExcelFile(
                webDtos,
                response,
                DateUtils.getCurrentYear()+"年有效专利IPC统计.xls",
                DateUtils.getCurrentYear()+"年有效专利IPC统计");
    }

    /**
     * 发明人统计
     */
    @ApiOperation(value ="发明人统计")
    @GetMapping("/inventorAnalysis")
    @ResponseBody
    public Res getInventorAnalysislList(HttpServletResponse response) throws Exception{
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("未查询到平台数据");
        }
        List<WebInventorAnalysisInfo> infos = rptPatentDataAreaService.getInventorAnalysislList(userInfo.getPlatformid());
        List<WebInventorAnalysisInfoDto> webDtos = webExcelExportService.getWebInventorAnalysisInfoDto(infos);
        return WebExcelUtils.exportGeneralExcelFile(
                webDtos,
                response,
                DateUtils.getCurrentYear()+"年有效专利发明人统计.xls",
                DateUtils.getCurrentYear()+"年有效专利发明人统计");
    }

    /**
     * 代理机构统计
     */
    @ApiOperation(value ="代理机构统计 1当前有效量，2专利总量，3今年公开量，4今年授权量")
    @GetMapping("/AgencyAnalysis/{type}")
    @ResponseBody
    public Res getAgencyAnalysislList(@PathVariable(value = "type")  Integer type,HttpServletResponse response) throws Exception{
        if (type == null){
            return Res.error("请求参数有误");
        }
        if (type !=1 && type !=2 && type!=3 && type!=4){
            return Res.error("请求参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("未查询到平台数据");
        }
        List<WebAgencyAnalysisInfo> infos = rptPatentDataAreaService.getAgencyAnalysislList(userInfo.getPlatformid(), type);
        List<WebAgencyAnalysisInfoDto> webDtos = webExcelExportService.getWebAgencyAnalysisInfoDto(infos);
        return WebExcelUtils.exportGeneralExcelFile(
                webDtos,
                response,
                DateUtils.getCurrentYear()+"年代理机构统计.xls",
                DateUtils.getCurrentYear()+"年代理机构统计");
    }
}
